package sf.database.jdbc.type;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.RowId;
import java.sql.SQLException;
import java.sql.Types;

/**
 * rowid为只读对象.
 */
public class RowIdType implements TypeHandler<RowId> {
    public RowId get(ResultSet rs, String colName) throws SQLException {
        return rs.getRowId(colName);
    }

    public RowId get(ResultSet rs, int index) throws SQLException {
        return rs.getRowId(index);
    }

    @Override
    public RowId get(CallableStatement cs, int index) throws SQLException {
        return cs.getRowId(index);
    }

    @Override
    public RowId get(CallableStatement cs, String parameterName) throws SQLException {
        return cs.getRowId(parameterName);
    }

    @Override
    public void update(ResultSet rs, String columnLabel, Object value) throws SQLException {
        rs.updateRowId(columnLabel, (RowId) value);
    }

    @Override
    public void update(ResultSet rs, int columnIndex, Object value) throws SQLException {
        rs.updateRowId(columnIndex, (RowId) value);
    }

    public Object set(PreparedStatement stat, Object obj, int i) throws SQLException {
        if (null == obj) {
            stat.setNull(i, getSqlType());
        } else {
            stat.setRowId(i, (RowId) obj);
        }
        return null;
    }

    public Object set(CallableStatement cs, Object obj, String parameterName) throws SQLException {
        if (null == obj) {
            cs.setNull(parameterName, getSqlType());
        } else {
            cs.setRowId(parameterName, (RowId) obj);
        }
        return null;
    }

    @Override
    public int getSqlType() {
        return Types.ROWID;
    }

    @Override
    public Class<RowId> getDefaultJavaType() {
        return RowId.class;
    }
}
